var height=hheight-362;
$(function(){
	$('#test').datagrid({
		width:'100%',
		height:height,
		nowrap: true,
		autoRowHeight: false,
		striped: true,
		collapsible:false,
		singleSelect:true,
		idField:'electronicCode',//标识字段 
		columns:[[
		          {title:'id',field:'id',hidden:true},
		          {title:'转运车条码',field:'electronicCode',width:'20%'},
		          {title:'送药工工号',field:'workerId',width:'20%'},
		          {title:'送药工姓名',field:'workerName',width:'20%'},
		          {title:'转运开始时间',field:'startTime',width:'20%'},
		          {title:'接收时间',field:'revTime',width:'19%'}
		          
		          ]],
		          pagination:false,
		          rownumbers:true,
		          onLoadError: function(){
		        	  window.location.href=basePath+"logoutSession.jsp";
		          }
	});
	var p = $('#test').datagrid('getPager');
	$(p).pagination({
		onBeforeRefresh:function(){
			
		}
	});
	
	//点击回车获取转运车相关信息
	$("input[name='addVehicleId']").keypress(function(event){
		if(event.keyCode==13){
			getTraDrugInfos();
		}
	});
	//点击回车删除表格里的与取消转运车条码有关记录
	$("input[name='delVehicleId']").keypress(function(event){
		if(event.keyCode==13){
			var trvalue= $("input[name='delVehicleId']").val();
			if(!trvalue ||trvalue == ""){
				alert("请扫描转运车条码");
				return false;
			}
			var rows=$('#test').datagrid('getRows');
			var flag=1;
			for(var i=0;i<rows.length;i++){
				if(rows[i].electronicCode==trvalue){
					var index=$('#test').datagrid('getRowIndex',rows[i]);
					$('#test').datagrid('deleteRow',index);
					i--; //防止出现误删
					flag=2;
				}
			}
			if (flag == 1) {
				alert("此转运车未扫描");
			}
			$("input[name='delVehicleId']").val('');
			$("input[name='addVehicleId']").val('').focus();
		}
		
	});
});
//异步获取此转运车相关信息
function getTraDrugInfos(){
	var info=$("input[name='addVehicleId']").val();
	if(!info ||info == ""){
		alert("请扫描转运车条码");
		return false;
	}
	$.ajax({
		type : "POST",
		dataType : "text",
		async : false,
		url : basePath + "isc/traDrugInfo_getListByTraTolCode.do?traTolCode=" + info,
		traditional : true,//在struts2下该属性必须有 
		success : function(result) {
			if(result != ""){
				var jsonObject=eval("("+result+")");
				var index1=$('#test').datagrid('getRowIndex',info);
				if(index1<0){
					for(var i=0;i<jsonObject.length;i++){
						$('#test').datagrid('insertRow', {
							index : 0, // 索引从0开始
							row : {
								id:jsonObject[i].id,
								electronicCode : jsonObject[i].traTolBarcode,
								workerId:        jsonObject[i].traCode,
								workerName:      jsonObject[i].traName,
								startTime:       jsonObject[i].traDate,
								revTime:         jsonObject[i].revDate
							}
						});
					}
				}else{
					alert("编号为：【"+info+"】的转运车已扫描!");
				}
			}else{
				alert("编号为：【"+info+"】的转运车不存在转运记录或者已被接收！");
			}
			$("input[name='addVehicleId']").val("").focus();
		}
	});
}


//点击保存将接收人的记录插入到对应的表格中
function saveNurseInfo(){
	var ids=[];
	var rows= $('#test').datagrid('getRows');
	if(rows.length>0){
		for(var i=0;i<rows.length;i++){
			if($.inArray(rows[i].id, ids)== -1){
				ids.push(rows[i].id);
			}
		}
	}else{
		alert("还未扫描转运车");
		return;
	}
	var idsStr=ids.join(",");
	$.ajax({
		type : "POST",
		dataType : "text",
		async : false,
		url : basePath + "isc/traDrugInfo_saveNurseInfo.do?infos="+idsStr,
		traditional : true,//在struts2下该属性必须有 
		success : function(result) {
			if(result=="success"){
				alert("保存成功！");
				$('#test').datagrid('loadData', {  rows: [] });//清空下方DateGrid
				$("input[name='delVehicleId']").val("");
				$("input[name='addVehicleId']").val("").focus();
			}else{
				alert("保存失败！");
			}
		}
	});
}